package com.sanyang.lawyer.controller.home;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sanyang.lawyer.entity.*;
import com.sanyang.lawyer.service.Audit.AuditService;
import com.sanyang.lawyer.service.Audit.Case_auditService;
import com.sanyang.lawyer.service.Case.Case_lawService;
import com.sanyang.lawyer.service.adm.AdmNoticeService;
import com.sanyang.lawyer.service.adm.SignService;
import com.sanyang.lawyer.service.system.UserService;
import com.sanyang.lawyer.utils.ActivitiUtils;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: lawyer
 * @description: 主页面的
 * @author: Mr.Wang
 * @create: 2019-10-31 16:15
 **/

@RestController
@RequestMapping("home")
public class HomeController {

    @Autowired
    private ActivitiUtils activitiUtils;

    @Autowired
    private Case_lawService caseLaw;

    @Autowired
    private AdmNoticeService admNotice;

    @Autowired
    private AuditService auditService;

    @Autowired
    private Case_lawService lawService;

    @Autowired
    private UserService userService;

    @Autowired
    private SignService signService;


    @RequestMapping("law")
    public List<Case_law> list(){
        List<Case_law> list=caseLaw.getlist();
        if(list.size()<=3){
            return list;
        }else{
            List<Case_law> list2=new ArrayList<Case_law>();
            Case_law l1=list.get(0);
            Case_law l2=list.get(1);
            Case_law l3=list.get(2);
            list2.add(l1);
            list2.add(l2);
            list2.add(l3);
            return list2;
        }
    }

    @RequestMapping("audit")
    public List<Audit> auditlist(){
        List<Audit> list=auditService.listall();
        if(list.size()<=3){
            return list;
        }else{
            List<Audit> list2=new ArrayList<Audit>();
            Audit l1=list.get(0);
            Audit l2=list.get(1);
            Audit l3=list.get(2);
            list2.add(l1);
            list2.add(l2);
            list2.add(l3);
            return list2;
        }
    }

    @RequestMapping("notice")
    public List<Adm_Notice> listall(){
        List<Adm_Notice> list=admNotice.listallcelc();
        if(list.size()<=3){
            return list;
        }else{
            List<Adm_Notice> list2=new ArrayList<Adm_Notice>();
            Adm_Notice l1=list.get(0);
            Adm_Notice l2=list.get(1);
            Adm_Notice l3=list.get(2);
            list2.add(l1);
            list2.add(l2);
            list2.add(l3);
            return list2;
        }
    }

    //审批中
    @RequestMapping("backlog")
    public String backlog(){
        List<Case_law> list=lawService.backlog();
        if(list.size()==0){
            return "0";
        }else{
            return list.size()+"";
        }
    }
    //待办
    @RequestMapping("backlogl")
    public String backlogl(){
        List<Case_law> list=lawService.backlogl();
        if(list.size()==0){
            return "0";
        }else{
            return list.size()+"";
        }
    }
    //首页显示查询
    @RequestMapping("settle")
    public String settle(){
        List<Case_law> list=lawService.settle();
        if(list.size()==0){
            return "0";
        }else{
            return list.size()+"";
        }
    }



    @RequestMapping("settle2")
    public String settle2(int userId){
        System_user user=userService.getById(userId);
        List<Task> list=activitiUtils.findTaskByUserName(user.getUserName());
        if(list.size()==0){
            return "0";
        }else{
            return list.size()+"";
        }
    }

    @RequestMapping("insert")
    public SignTm signinsert(int userId) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        SimpleDateFormat cf = new SimpleDateFormat(" HH:mm:ss");//设置日期格式

        SignTime si=new SignTime();
        si.setUserid(userId);
        si.setDate(df.format(new Date()));
        List<Sign> signlist = signService.signlist(si);
        if(signlist.size()!=0){
            return null;
        }else{
            System_user user=userService.getById(userId);
            Sign sign=new Sign();
            sign.setUserId(user);
            sign.setSignDate(df.format(new Date()));
            sign.setSignTime(cf.format(new Date()));


            String kdate="08:00:00";
            String jdate=cf.format(new Date());
            String DATE_FORMAT_YMDHMS_WITH_T="HH:mm:ss";
            SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YMDHMS_WITH_T);
            Date date1 = format.parse(kdate);
            Date date2 = format.parse(jdate);
            //计算差值，分钟数
            long minutes = (date1.getTime() - date2.getTime()) / (1000 * 60);
            if(minutes>=0){
                sign.setSignZt("正常");
            }else if(minutes<0&&minutes<-240){
                sign.setSignZt("旷工");
            }else{
                sign.setSignZt("迟到");
            }
            SignTm sm=new SignTm();
            sm.setUname(user.getUserName());
            sm.setState(sign.getSignZt());
            sm.setTime(Math.abs(minutes)+"");
            System.out.println(sm.toString());
            signService.insert(sign);
            return sm;
        }
    }




}
